package com.yuelao.yuelao_backend.controller;

import com.yuelao.yuelao_backend.common.ApiResponse;
import com.yuelao.yuelao_backend.dto.CoinTransactionDTO;
import com.yuelao.yuelao_backend.dto.request.SpendCoinsRequest;
import com.yuelao.yuelao_backend.dto.request.TransferCoinsRequest;
import com.yuelao.yuelao_backend.service.CoinService;
import com.yuelao.yuelao_backend.util.UserContextUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 爱心币Controller
 * 重构说明：适配新的CoinService，金额单位为分（1元=100分）
 */
@RestController
@RequestMapping("/coins")
@Slf4j
@RequiredArgsConstructor
public class CoinController {

    private final CoinService coinService;

    /**
     * 获取爱心币余额和等级
     * GET /coins/balance
     * @return 余额和等级信息
     */
    @GetMapping("/balance")
    public ApiResponse<Map<String, Object>> getCoinBalance() {
        Long userId = UserContextUtil.getCurrentUserId();
        Map<String, Object> balanceInfo = coinService.getCoinBalance(userId);
        return ApiResponse.ok(balanceInfo);
    }

    /**
     * 获取交易记录
     * GET /coins/transactions
     * @param page 页码，默认1
     * @param size 每页数量，默认10
     * @param type 交易类型，可选'earn', 'spend', 'transfer_in', 'transfer_out'
     * @return 交易记录列表
     */
    @GetMapping("/transactions")
    public ApiResponse<List<CoinTransactionDTO>> getTransactionHistory(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String type) {
        Long userId = UserContextUtil.getCurrentUserId();
        List<CoinTransactionDTO> transactions = coinService.getTransactionHistory(userId, page, size, type);
        return ApiResponse.ok(transactions);
    }

    /**
     * 消费爱心币
     * POST /coins/spend
     * @param request 消费请求
     * @return 是否成功
     */
    @PostMapping("/spend")
    public ApiResponse<Boolean> spendCoins(@Valid @RequestBody SpendCoinsRequest request) {
        Long userId = UserContextUtil.getCurrentUserId();
        boolean result = coinService.spendCoins(
            userId,
            request.getAmount(),
            request.getSource(),
            request.getTitle(),
            request.getDescription(),
            request.getRelatedId(),
            request.getRelatedType()
        );
        return ApiResponse.ok(result);
    }

    /**
     * 转账爱心币
     * POST /coins/transfer
     * @param request 转账请求
     * @return 是否成功
     */
    @PostMapping("/transfer")
    public ApiResponse<Boolean> transferCoins(@Valid @RequestBody TransferCoinsRequest request) {
        Long userId = UserContextUtil.getCurrentUserId();
        boolean result = coinService.transferCoins(
            userId,
            request.getToUserId(),
            request.getAmount(),
            request.getRemark()
        );
        return ApiResponse.ok(result);
    }

    /**
     * 升级爱心币等级
     * POST /coins/upgrade-level
     * @return 升级结果
     */
    @PostMapping("/upgrade-level")
    public ApiResponse<Map<String, Object>> upgradeLevel() {
        Long userId = UserContextUtil.getCurrentUserId();
        Map<String, Object> result = coinService.upgradeLevel(userId);
        return ApiResponse.ok(result);
    }
}
